<?php

declare (strict_types=1);
namespace App\Model;

use Carbon\Carbon;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $child_id 
 * @property string $name 
 * @property string $birth 
 * @property string $photo 
 * @property string $parent_name 
 * @property string $parent_mobile 
 * @property string $family_address 
 * @property string $add_tiem 
 */
class Child extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'child';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name','birth','photo','parent_name','parent_mobile','family_address'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['child_id' => 'integer'];

    protected $primaryKey = "child_id";

    public $timestamps = false;

    protected $appends = ['age'];

    protected $hidden = ['pivot'];

    public function getAgeAttribute(){
        return Carbon::parse($this->getAttribute("birth"))->diffInYears();
    }

    public function users(){
        return $this->belongsToMany(User::class,'child_user_mid','child_id','user_id');
    }

    public function family(){
        return $this->hasMany(ChildUserMid::class,'child_id','child_id');
    }
}